﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
using System.Configuration;

namespace WebCumpleanios
{
    public partial class sendPostal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                if ((Page.Request.Params["id"] != null) && (Page.Request.Params["email"] != null))
                {
                    string ID = Page.Request.Params["id"];
                    string email = Page.Request.Params["email"];


                    List<string> emails = new List<string>();
                    emails.Add(email);
                    string path = Server.MapPath(@"fla\imagenes\") + ID + "_" + DateTime.Now.Year + ".jpg";
                    EnviarMails(emails, "Postal de cumpleaños", "Tus amigos de FONAFE te deseamos un feliz cumpleaños", path);
                }
            }
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {

        }

        public void EnviarMails(List<string> Destinatario, string Asunto, string Body, string path)
        {
            bool EnabledSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnabledSsl"]);
            string bccMail = Convert.ToString(ConfigurationManager.AppSettings["bccMail"]);
            string mailRemitente = ConfigurationManager.AppSettings["mailRemitente"];
            string NombreRemitente = ConfigurationManager.AppSettings["NombreRemitente"];
            string CorreoRemitente = ConfigurationManager.AppSettings["mailRemitente"];

            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

            foreach (string item in Destinatario)
            {
                msg.To.Add(new MailAddress(item, "", Encoding.UTF8));
            }

            msg.From = new MailAddress(CorreoRemitente, NombreRemitente, System.Text.Encoding.UTF8);
            msg.Subject = Asunto;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = Body;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;
            Attachment postalAttach = new Attachment(path, "image/jpeg");
            msg.Attachments.Add(postalAttach);
            SmtpClient client = new SmtpClient();
            client.EnableSsl = EnabledSsl;

            try
            {
                client.Send(msg);
                lblRespuesta.CssClass = "mensajeOk";
                lblRespuesta.Text = "Se envió satisfactoriamente la postal.";
            }
            catch (Exception ex)
            {
                lblRespuesta.CssClass = "mensajeError";
                lblRespuesta.Text = "Ocurrió un error al enviar la postal. " + ex.Message + ". Intentelo nuevamente.";
                Utils.WriteEventLog(ex);
            }
        }
    }
}